package com.cy.boot.module.system.controller.admin.temp;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.cy.boot.framework.common.pojo.PageParam;
import com.cy.boot.framework.common.pojo.PageResult;
import com.cy.boot.framework.common.pojo.CommonResult;
import com.cy.boot.framework.common.util.object.BeanUtils;
import static com.cy.boot.framework.common.pojo.CommonResult.success;

import com.cy.boot.framework.excel.core.util.ExcelUtils;

import com.cy.boot.framework.operatelog.core.annotations.OperateLog;
import static com.cy.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cy.boot.module.system.controller.admin.temp.vo.*;
import com.cy.boot.module.system.dal.dataobject.temp.TempDO;
import com.cy.boot.module.system.service.temp.TempService;

@Tag(name = "管理后台 - 测试用代码生成专用")
@RestController
@RequestMapping("/system/temp")
@Validated
public class TempController {

    @Resource
    private TempService tempService;

    @PostMapping("/create")
    @Operation(summary = "创建测试用代码生成专用")
    @PreAuthorize("@ss.hasPermission('system:temp:create')")
    public CommonResult<Long> createTemp(@Valid @RequestBody TempSaveReqVO createReqVO) {
        return success(tempService.createTemp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新测试用代码生成专用")
    @PreAuthorize("@ss.hasPermission('system:temp:update')")
    public CommonResult<Boolean> updateTemp(@Valid @RequestBody TempSaveReqVO updateReqVO) {
        tempService.updateTemp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除测试用代码生成专用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:temp:delete')")
    public CommonResult<Boolean> deleteTemp(@RequestParam("id") Long id) {
        tempService.deleteTemp(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得测试用代码生成专用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:temp:query')")
    public CommonResult<TempRespVO> getTemp(@RequestParam("id") Long id) {
        TempDO temp = tempService.getTemp(id);
        return success(BeanUtils.toBean(temp, TempRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得测试用代码生成专用分页")
    @PreAuthorize("@ss.hasPermission('system:temp:query')")
    public CommonResult<PageResult<TempRespVO>> getTempPage(@Valid TempPageReqVO pageReqVO) {
        PageResult<TempDO> pageResult = tempService.getTempPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TempRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出测试用代码生成专用 Excel")
    @PreAuthorize("@ss.hasPermission('system:temp:export')")
    @OperateLog(type = EXPORT)
    public void exportTempExcel(@Valid TempPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TempDO> list = tempService.getTempPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "测试用代码生成专用.xls", "数据", TempRespVO.class,
                        BeanUtils.toBean(list, TempRespVO.class));
    }

}